class TFHGunFire extends TFProjectileFire;

var() float MaxRollSpeed;
var() float RollSpeed;
var() float BarrelRotationsPerSec;
var() int   RoundsPerRotation;
var() float FireTime;
var() Sound WindingSound;
var() Sound FiringSound;
var() byte	MinigunSoundVolume;
var TFGunnerClass Gun;
var() float WindUpTime;

var() String FiringForce;
var() String WindingForce;

function StartBerserk()
{

}

function StopBerserk()
{

}

function StartSuperBerserk()
{

}

function PostBeginPlay()
{
    Super.PostBeginPlay();
    calcfirerate();
    Gun = TFGunnerClass(Owner);
}

function FlashMuzzleFlash()
{
    local rotator r;
    r.Roll = Rand(65536);
    Weapon.SetBoneRotation('Bone_Flash', r, 0, 1.f);
    Super.FlashMuzzleFlash();
}

function InitEffects()
{
    Super.InitEffects();
    if ( FlashEmitter != None )
		Weapon.AttachToBone(FlashEmitter, 'flash');
}

function PlayAmbientSound(Sound aSound)
{

}

function StopRolling()
{
    RollSpeed = 0.f;
    Gun.RollSpeed = 0.f;
}

function PlayFiring()
{
    local rotator rot;

    rot=instigator.Controller.rotation;

    rot.pitch+=(ShakeOffsetMag.z*frand()*100)-ShakeOffsetMag.z*35;
    rot.yaw+=(ShakeOffsetMag.x*frand()*100)-ShakeOffsetMag.x*50;

    instigator.Controller.setrotation(rot);
    Weapon.PlayOwnedSound(sound'TFMedia.Gunner.HGFire', SLOT_None, 7, true, 600, 0.9+frand()*0.02);
}

function ServerPlayFiring()
{
    Weapon.PlayOwnedSound(sound'TFMedia.Gunner.HGFire', SLOT_None, 7, true, 600, 0.9+frand()*0.02);
}

function PlayPreFire() {}
function PlayStartHold() {}
function PlayFireEnd() {}
function StartFiring();
function StopFiring();

function CalcFirerate()
{
    FireRate = 1.f / (RoundsPerRotation * BarrelRotationsPerSec);
    MaxRollSpeed = 131072.f*BarrelRotationsPerSec;
}

function bool IsIdle()
{
	return false;
}

auto state Idle
{
	function bool IsIdle()
	{
		return true;
	}

    function BeginState()
    {
        PlayAmbientSound(None);
        StopRolling();
    }

    function EndState()
    {
        PlayAmbientSound(WindingSound);
    }

    function StartFiring()
    {
        RollSpeed = 0;
		FireTime = (RollSpeed/MaxRollSpeed) * WindUpTime;
        GotoState('WindUp');
    }
}

state WindUp
{
    function BeginState()
    {
        if(tfhgunsiegem(weapon.getfiremode(1)).bSiegeMode)
        {
            ShakeOffsetMag/=25;
            spread=250;
            ProjPerFire=2;
            BarrelRotationsPerSec=3;
        }
        else
        {
            ShakeOffsetMag=default.ShakeOffsetMag;
            spread=default.spread;
            ProjPerFire=default.ProjPerFire;
            BarrelRotationsPerSec=default.BarrelRotationsPerSec;
        }
        calcfirerate();

        Weapon.PlayOwnedSound(Sound'TFMedia.Gunner.HGwindup',SLOT_Interact,1.5);
        ClientPlayForceFeedback(WindingForce);  // jdf
    }

    function EndState()
    {
        if (ThisModeNum == 0)
        {
            if ( (Weapon == None) || !Weapon.GetFireMode(1).bIsFiring )
                StopForceFeedback(WindingForce);
        }
        else
        {
            if ( (Weapon == None) || !Weapon.GetFireMode(0).bIsFiring )
                StopForceFeedback(WindingForce);
        }
    }

    function ModeTick(float dt)
    {
        FireTime += dt;
        RollSpeed = (FireTime/WindUpTime) * MaxRollSpeed;

        if ( !bIsFiring )
        {
			GotoState('WindDown');
			return;
		}

        if (RollSpeed >= MaxRollSpeed)
        {
            RollSpeed = MaxRollSpeed;
            FireTime = WindUpTime;
            Gun.UpdateRoll(dt, RollSpeed, ThisModeNum);
			GotoState('FireLoop');
            return;
        }

        Gun.UpdateRoll(dt, RollSpeed, ThisModeNum);
    }

    function StopFiring()
    {
        GotoState('WindDown');
    }
}

state FireLoop
{
    function BeginState()
    {
        NextFireTime = Level.TimeSeconds - 0.1; //fire now!
        PlayAmbientSound(FiringSound);
        ClientPlayForceFeedback(FiringForce);  // jdf
        Gun.LoopAnim(FireLoopAnim, FireLoopAnimRate, TweenTime);
        Gun.SpawnShells(RoundsPerRotation*BarrelRotationsPerSec);
    }

    function StopFiring()
    {
        GotoState('WindDown');
    }

    function EndState()
    {
        PlayAmbientSound(WindingSound);
        StopForceFeedback(FiringForce);  // jdf
        Gun.LoopAnim(Gun.IdleAnim, Gun.IdleAnimRate, TweenTime);
        Gun.SpawnShells(0.f);
     }

    function ModeTick(float dt)
    {
        Super.ModeTick(dt);
        Gun.UpdateRoll(dt, RollSpeed, ThisModeNum);
        if ( !bIsFiring )
        {
			GotoState('WindDown');
			return;
		}
    }
}

state WindDown
{
    function BeginState()
    {
        Weapon.PlayOwnedSound(Sound'TFMedia.Gunner.HGwinddown',SLOT_Interact,1.5);
        ClientPlayForceFeedback(WindingForce);  // jdf
    }

    function EndState()
    {
        if (ThisModeNum == 0)
        {
            if ( (Weapon == None) || !Weapon.GetFireMode(1).bIsFiring )
                StopForceFeedback(WindingForce);
        }
        else
        {
            if ( (Weapon == None) || !Weapon.GetFireMode(0).bIsFiring )
                StopForceFeedback(WindingForce);
        }
    }

    function ModeTick(float dt)
    {
        FireTime -= dt;
        RollSpeed = (FireTime/WindUpTime) * MaxRollSpeed;

        if (RollSpeed <= 0.f)
        {
            RollSpeed = 0.f;
            FireTime = 0.f;
            Gun.UpdateRoll(dt, RollSpeed, ThisModeNum);
            GotoState('Idle');
            return;
        }

        Gun.UpdateRoll(dt, RollSpeed, ThisModeNum);
    }

    function StartFiring()
    {
        GotoState('WindUp');
    }
}

defaultproperties
{
     BarrelRotationsPerSec=1.800000
     RoundsPerRotation=5
     WindUpTime=1.800000
     FiringForce="minifireb"
     WindingForce="miniempty"
     ProjSpawnOffset=(X=8.000000,Y=10.000000,Z=-7.000000)
     bPawnRapidFireAnim=True
     PreFireTime=1.800000
     FireLoopAnimRate=9.000000
     AmmoClass=Class'taskforces.TFHGunAmmo'
     AmmoPerFire=1
     ShakeRotMag=(X=75.000000,Y=75.000000,Z=75.000000)
     ShakeRotRate=(X=10000.000000,Y=10000.000000,Z=10000.000000)
     ShakeRotTime=2.000000
     ShakeOffsetMag=(X=13.000000,Y=13.000000,Z=16.000000)
     ShakeOffsetRate=(X=5000.000000,Y=5000.000000,Z=5000.000000)
     ShakeOffsetTime=2.000000
     ProjectileClass=Class'taskforces.TFHGunProj'
     BotRefireRate=0.990000
     FlashEmitterClass=Class'taskforces.TFHGunMuz1st'
     SmokeEmitterClass=Class'XEffects.MinigunMuzzleSmoke'
     aimerror=900.000000
     Spread=1200.000000
     SpreadStyle=SS_Random
}
